package study.nosql.redis.consumer;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.Topic;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

/**
 * 发布/订阅 实现队列
 */
@Slf4j
//@Component
public class SubQueConsumer extends MessageListenerAdapter {

    private static final Topic TOPIC = new ChannelTopic("queue_pub_test");

    public SubQueConsumer() {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.addMessageListener(this, TOPIC);
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        String channel = StrUtil.utf8Str(message.getChannel());
        String body = StrUtil.utf8Str(message.getBody());
        log.info(">>>>>> channel= {}, body= {}", channel, body);
    }
}
